package ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MyReentrantLock {

    private int data = 100;
    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    private final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    private final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();
    public int readData(){
        readLock.lock();
        try {
            return data;
        }finally {
            readLock.unlock();
        }
    }

    public void writeData(int data){
        writeLock.lock();
        try {
            this.data = data;
        }finally {
            writeLock.unlock();
        }
    }
}
